package com.ruoyi.bearing.controller;

import com.ruoyi.bearing.domain.BearingModel;
import com.ruoyi.bearing.domain.BearingSignal;
import com.ruoyi.bearing.service.IBearingSignalService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.exception.flask.FlaskException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 信号分析
 */
@Api("信号分析")
@RestController
@RequestMapping("/bearingData/signal")
public class BearingSignalController extends BaseController {
    @Autowired
    private IBearingSignalService bearingSignalService;

    /**
     * 信号分析
     */
    @ApiOperation("信号分析")
    @PreAuthorize("@ss.hasPermi('bearingData:signal:analysis')")
    @PostMapping("/analysis")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult analysis(@RequestParam("files") MultipartFile[] files, BearingModel bearingModel) throws IOException, IllegalAccessException {
        String res = bearingSignalService.startBearingSignalAnalysis(files[0], bearingModel);
        if (null == res) {
            throw new FlaskException();
        }
        if ("fileError".equals(res)) {
            throw new BaseException("请检查文件格式");
        }
        return success(res);
    }

    /**
     * 查询信号分析结果
     */
    @ApiOperation("查询信号分析结果")
    @PreAuthorize("@ss.hasPermi('bearingData:signal:list')")
    @GetMapping("/list")
    @Transactional(rollbackFor = Exception.class)
    public TableDataInfo list(BearingSignal bearingSignal) {
        startPage();
        List<BearingSignal> bearingSignalList = bearingSignalService.selectBearingSignal(bearingSignal);
        return getDataTable(bearingSignalList);
    }

    /**
     * 删除信号分析结果
     */
    @PreAuthorize("@ss.hasPermi('bearingData:signal:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bearingSignalService.deleteAnalysisResultByIds(ids));
    }

    /**
     * 下载信号分析结果pdf
     */
    @ApiOperation("下载信号分析结果")
    @PreAuthorize("@ss.hasPermi('bearingData:signal:download')")
    @PostMapping("/download")
    public void download(HttpServletResponse response, BearingSignal bearingSignal) {
        bearingSignalService.downloadAnalysisResult(response,bearingSignal);
    }

}
